﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FriendStar.Application.MainData;
using FriendStar.Application.WareHouse;
using FriendStar.Code;
using FriendStar.Domain.Entity.WareHouse;
using FriendStar.Repository.Infrastructure;
using FriendStar.Application.BOM;

namespace FriendStar.Web.Areas.WareHouse.Controllers
{
    public class MaterialStoreController : ControllerBase
    {
        //
        // GET: /WareHouse/MaterialStore/
        private string[] MaterialStoreEnterType = { "LR", "LZ", "LY", "LF", "LI" };
        private string[] MaterialStoreOutType = { "LC", "LO", "LW", "LU"};
        private string[] MaterialStoreInventoryType = { "LP" };
        private Dictionary<string, string> MaterialStoreTypeDictionary = new Dictionary<string, string>()
        {
            {"LR", "材料采购入库"},
            {"LZ", "材料自购入库"},
            {"LY", "材料余料入库"},
            {"LF", "材料废料入库"},
            {"LI", "材料调拨入库"},
            {"LC", "材料采购出库"},
            {"LO", "材料调拨出库"},
            {"LW", "材料废料出库"},
            {"LU", "材料自购出库"},
            {"LP", "材料盘点"},
        };

        private string UseUnit = Configs.GetValue("UseUnit");
        private string FactoryFlag = Configs.GetValue("FactoryFlag");

        CLDE_ZD_FL_CLDEerialClassNOApp clde_zd_fl_cldeerialclassnoApp = new CLDE_ZD_FL_CLDEerialClassNOApp();

        BUS_SJ_DictionaryApp bus_sj_dictionaryApp = new BUS_SJ_DictionaryApp();
        ZSJ_B1_GH_SupplierInformationApp zsj_b1_gh_supplierinformationApp = new ZSJ_B1_GH_SupplierInformationApp();
        CPK_ZD_11_DatStoreHouseApp cpk_zd_11_datstorehouseApp = new CPK_ZD_11_DatStoreHouseApp();
        CLK_ZD_14_MaterialStoreHouseSetApp clk_zd_14_materialstorehousesetApp = new CLK_ZD_14_MaterialStoreHouseSetApp();
        V_CLK_91_GH_SuMaterialApp v_clk_91_gh_sumaterialApp = new V_CLK_91_GH_SuMaterialApp();
        V_MCG_11_HT_MaterialPurchaseDJ_DAApp v_mcg_11_ht_materialpurchasedj_daApp = new V_MCG_11_HT_MaterialPurchaseDJ_DAApp();
        V_CLK_61_PC_MaterialProAIAOAccoApp v_clk_61_pc_materialproaiaoaccoApp = new V_CLK_61_PC_MaterialProAIAOAccoApp();

        V_CLK_42_MX_MaterialProListAccoApp v_clk_42_mx_materialprolistaccoApp = new V_CLK_42_MX_MaterialProListAccoApp();
        V_CLK_44_MX_MaterialProListAcco_MidApp v_clk_44_mx_materialprolistacco_midApp = new V_CLK_44_MX_MaterialProListAcco_MidApp();
        V_CLK_46_MX_MaterialProListAcco_ArcApp v_clk_46_mx_materialprolistacco_arcApp = new V_CLK_46_MX_MaterialProListAcco_ArcApp();
        CLK_42_MX_MaterialProListAccoApp clk_42_mx_materialprolistaccoApp = new CLK_42_MX_MaterialProListAccoApp();

        V_CLK_11_DJ_MaterialRegisterApp v_clk_11_dj_materialregisterApp = new V_CLK_11_DJ_MaterialRegisterApp();
        V_CLK_12_DJ_MaterialRegister_MidApp v_clk_12_dj_materialregister_midApp = new V_CLK_12_DJ_MaterialRegister_MidApp();
        V_CLK_21_CC_MaterialStorageCardApp v_clk_21_cc_materialstoragecardApp = new V_CLK_21_CC_MaterialStorageCardApp();
        V_CLK_22_CC_MaterialStorageCard_MidApp v_clk_22_cc_materialstoragecard_midApp = new V_CLK_22_CC_MaterialStorageCard_MidApp();
        V_CLK_23_CC_MaterialStorageCard_ArcApp v_clk_23_cc_materialstoragecard_arcApp = new V_CLK_23_CC_MaterialStorageCard_ArcApp();
        V_CLK_31_BL_MaterialPreMaterialApp v_clk_31_bl_materialprematerialApp = new V_CLK_31_BL_MaterialPreMaterialApp();
        V_CLK_32_BL_MaterialPreMaterial_MidApp v_clk_32_bl_materialprematerial_midApp = new V_CLK_32_BL_MaterialPreMaterial_MidApp();
        V_CLK_33_BL_MaterialPreMaterial_ArcApp v_clk_33_bl_materialprematerial_arcApp = new V_CLK_33_BL_MaterialPreMaterial_ArcApp();

        MaterialStoreApp app = new MaterialStoreApp();

        #region 来料
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult MaterialStoreNotice(string status)
        {
            status = string.IsNullOrEmpty(status) ? "0" : status;
            ViewBag.status = status;
            ViewBag.fhappencomList = zsj_b1_gh_supplierinformationApp.GetList("IsMaterial = '1'");
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaterialStoreNoticeListJson(Pagination pagination, string FHappenCompanyNO, string MaterialCode, string MaterialSizes, string FCertificateNO, string FVoucherNo, string FBatchNo, string FCurDate)
        {
            var data = new
            {
                rows = v_clk_11_dj_materialregisterApp.GetList(pagination, MaterialStoreEnterType, FHappenCompanyNO, MaterialCode, MaterialSizes, FCertificateNO, FVoucherNo, FBatchNo, FCurDate),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaterialStoreNoticeDocListJson(Pagination pagination, string FHappenCompanyNO, string MaterialCode, string MaterialSizes, string FCertificateNO, string FVoucherNo, string FBatchNo, string FCurDate)
        {
            var data = new
            {
                rows = v_clk_12_dj_materialregister_midApp.GetList(pagination, MaterialStoreEnterType, FHappenCompanyNO, MaterialCode, MaterialSizes, FCertificateNO, FVoucherNo, FBatchNo, FCurDate),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult MaterialStoreNoticeOrder()
        {
            ViewBag.fhappencomList = zsj_b1_gh_supplierinformationApp.GetList("IsMaterial = '1'");
            ViewBag.fdocutypenoList = bus_sj_dictionaryApp.GetItemList(MaterialStoreEnterType);
            ViewBag.materialnoList = clde_zd_fl_cldeerialclassnoApp.GetList("", "");
            ViewBag.fprocompanyList = zsj_b1_gh_supplierinformationApp.GetList("IsMaterial = '1'");
            return View();
        }

        #endregion

        #region 存储
        public ActionResult MaterialStoreStorage(string status)
        {
            status = string.IsNullOrEmpty(status) ? "0" : status;
            ViewBag.status = status;
            ViewBag.fhappencomList = zsj_b1_gh_supplierinformationApp.GetList("IsMaterial = '1'");
            ViewBag.fstoreList = cpk_zd_11_datstorehouseApp.GetSelectList().FindAll(t => t.FStoreType == "P2" || t.FStoreType == "P9");
            ViewBag.accountDate = GetDocumentNum.GetNum(UseUnit, FactoryFlag, "台账日期原材料", OperatorProvider.Provider.GetCurrent().UserCode, "", "临时");
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaterialStoreStorageListJson(Pagination pagination, string FDocuNO, string FHappenCompanyNO, string MaterialCode, string MaterialSizes, string FCertificateNO, string FVoucherNo, string FBatchNo, string FStoreNO, string FCurDate, string FAccountDate)
        {
            var data = new
            {
                rows = v_clk_21_cc_materialstoragecardApp.GetList(pagination, MaterialStoreEnterType, FDocuNO, FHappenCompanyNO, MaterialCode, MaterialSizes, FCertificateNO, FVoucherNo, FBatchNo, FStoreNO, FCurDate, FAccountDate),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaterialStoreStorageDocListJson(Pagination pagination, string FDocuNO, string FHappenCompanyNO, string MaterialCode, string MaterialSizes, string FCertificateNO, string FVoucherNo, string FBatchNo, string FStoreNO, string FCurDate, string FAccountDate)
        {
            string AccountDate = GetDocumentNum.GetNum(UseUnit, FactoryFlag, "台账日期原材料", OperatorProvider.Provider.GetCurrent().UserCode, "", "临时");
            if (AccountDate == FAccountDate)
            {
                var data = new
                {
                    rows = v_clk_22_cc_materialstoragecard_midApp.GetList(pagination, MaterialStoreEnterType, FDocuNO, FHappenCompanyNO, MaterialCode, MaterialSizes, FCertificateNO, FVoucherNo, FBatchNo, FStoreNO, FCurDate, FAccountDate),
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());
            }
            else
            {
                var data = new
                {
                    rows = v_clk_23_cc_materialstoragecard_arcApp.GetList(pagination, MaterialStoreEnterType, FDocuNO, FHappenCompanyNO, MaterialCode, MaterialSizes, FCertificateNO, FVoucherNo, FBatchNo, FStoreNO, FCurDate, FAccountDate),
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());
            }
        }

        #endregion

        #region 原材料入库
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult MaterialStoreEnter(string status)
        {
            status = string.IsNullOrEmpty(status) ? "0" : status;
            ViewBag.status = status;
            ViewBag.fhappencomList = zsj_b1_gh_supplierinformationApp.GetList("IsMaterial = '1'");
            ViewBag.fstoreList = cpk_zd_11_datstorehouseApp.GetSelectList().FindAll(t => t.FStoreType == "P2" || t.FStoreType == "P9");
            ViewBag.accountDate = GetDocumentNum.GetNum(UseUnit, FactoryFlag, "台账日期原材料", OperatorProvider.Provider.GetCurrent().UserCode, "", "临时");
            return View();
        }
        
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaterialStoreEnterListJson(Pagination pagination, string FDocuNO, string FHappenComNO, string MaterialCode, string MaterialSizes, string FStoreNO, string FCurDate, string FCredence, string FCertificateListID, string FAccountDate)
        {
            var data = new
            {
                rows = v_clk_42_mx_materialprolistaccoApp.GetList(pagination, MaterialStoreEnterType, FAccountDate, FDocuNO, FHappenComNO, MaterialCode, MaterialSizes, FStoreNO, FCurDate, FCredence, FCertificateListID),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaterialStoreEnterDocListJson(Pagination pagination, string FDocuNO, string FHappenComNO, string MaterialCode, string MaterialSizes, string FStoreNO, string FCurDate, string FCredence, string FCertificateListID, string FAccountDate)
        {
            string AccountDate = GetDocumentNum.GetNum(UseUnit, FactoryFlag, "台账日期原材料", OperatorProvider.Provider.GetCurrent().UserCode, "", "临时");
            if (AccountDate == FAccountDate)
            {
                var data = new
                {
                    rows = v_clk_44_mx_materialprolistacco_midApp.GetList(pagination, MaterialStoreEnterType, FAccountDate, FDocuNO, FHappenComNO, MaterialCode, MaterialSizes, FStoreNO, FCurDate, FCredence, FCertificateListID),
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());
            }
            else
            {
                var data = new
                {
                    rows = v_clk_46_mx_materialprolistacco_arcApp.GetList(pagination, MaterialStoreEnterType, FAccountDate, FDocuNO, FHappenComNO, MaterialCode, MaterialSizes, FStoreNO, FCurDate, FCredence, FCertificateListID),
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());
            }
        }
        
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult MaterialStoreEnterInput(string status, string number)
        {
            ViewBag.status = status != "B" ? "I" : status;
            ViewBag.number = (number == null) ? "" : number;
            ViewBag.fhappencomList = zsj_b1_gh_supplierinformationApp.GetList("IsMaterial = '1'");
            ViewBag.fdocutypenoList = bus_sj_dictionaryApp.GetItemList(MaterialStoreEnterType);
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult MaterialStoreEnterOrder()
        {
            ViewBag.fdocutypenoList = bus_sj_dictionaryApp.GetItemList(MaterialStoreEnterType);
            ViewBag.fhappencomList = zsj_b1_gh_supplierinformationApp.GetList("IsMaterial = '1'");
            ViewBag.materialnoList = clde_zd_fl_cldeerialclassnoApp.GetList("", "");
            ViewBag.fprocompanyList = zsj_b1_gh_supplierinformationApp.GetList("IsMaterial = '1'");
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult MaterialStoreEnterOrderEdit(string FDocuListID)
        {
            ViewBag.editObject = v_clk_42_mx_materialprolistaccoApp.GetForm(FDocuListID).ToJson();
            ViewBag.fdocutypenoList = bus_sj_dictionaryApp.GetItemList(MaterialStoreEnterType);
            ViewBag.fhappencomList = zsj_b1_gh_supplierinformationApp.GetList("IsMaterial = '1'");
            ViewBag.materialnoList = clde_zd_fl_cldeerialclassnoApp.GetList("", "");
            ViewBag.fprocompanyList = zsj_b1_gh_supplierinformationApp.GetList("IsMaterial = '1'");
            return View();
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitMaterialStoreEnterForm(string status, CLK_41_ML_MaterialProConAccoEntity entity, string detail, string keyValue)
        {
            string FFacNum = "";
            string DocNum = "";
            CLK_42_MX_MaterialProListAccoEntity detailEntity = null;
            if (entity != null)
            {
                if (entity.FDocuTypeNO != null && MaterialStoreTypeDictionary.ContainsKey(entity.FDocuTypeNO))
                {
                    FFacNum = MaterialStoreTypeDictionary[entity.FDocuTypeNO];
                }
                entity.FAccountDate = GetDocumentNum.GetNum(UseUnit, FactoryFlag, "台账日期原材料", OperatorProvider.Provider.GetCurrent().UserCode, "", "正式");
                detailEntity = detail.ToObject<CLK_42_MX_MaterialProListAccoEntity>();
            }
            if (status == "I")
            {
                if (!string.IsNullOrEmpty(entity.FDocuNO))
                {
                    var list = clk_42_mx_materialprolistaccoApp.GetList(entity.FDocuNO, detailEntity.MaterialNO, detailEntity.MaterialCode, detailEntity.MaterialSizes, detailEntity.StandardNumber, detailEntity.MaterialDemand);
                    if (list.Count > 0)
                    {
                        return Error("当前单据中已存在相同的原材料信息，入库信息输入操作失败。");
                    }
                }
                bool result = false;
                result = FriendStar.Repository.Infrastructure.ExecProcedure.InOutStoYclkStockCataInsert(UseUnit, entity, detailEntity, OperatorProvider.Provider.GetCurrent().UserCode, FactoryFlag, FFacNum, "正式", out DocNum);
                if (!string.IsNullOrEmpty(DocNum) && string.IsNullOrEmpty(entity.FDocuNO))
                {
                    entity.FDocuNO = DocNum;
                }
                if (result)
                {
                    return Success("入库信息输入操作成功。", entity);
                }
                else
                {
                    return Error("入库信息输入操作失败。");
                }
            }
            else if (status == "D")
            {
                bool result = false;
                result = FriendStar.Repository.Infrastructure.ExecProcedure.InOutStoYclkStockCataDelete(keyValue, OperatorProvider.Provider.GetCurrent().UserCode);
                if (result)
                {
                    return Success("入库信息删除操作成功。", entity);
                }
                else
                {
                    return Error("入库信息删除操作失败。");
                }
            }
            else if (status == "U")
            {
                bool result = false;
                result = FriendStar.Repository.Infrastructure.ExecProcedure.InOutStoYclkStockCataUpdate(detailEntity.FDocuListID, detailEntity.FQuantity, detailEntity.FPrice, detailEntity.FZS, detailEntity.FDescription, OperatorProvider.Provider.GetCurrent().UserCode);
                if (result)
                {
                    return Success("入库信息修改操作成功。", entity);
                }
                else
                {
                    return Error("入库信息修改操作失败。");
                }
            }
            else if (status == "P")
            {
                bool result = false;
                foreach (string item in keyValue.Split(','))
                {
                    result = FriendStar.Repository.Infrastructure.ExecProcedure.InOutStoYclkStockCataCheck(UseUnit, item, OperatorProvider.Provider.GetCurrent().UserCode);
                }
                if (result)
                {
                    return Success("入库信息审核操作成功。", entity);
                }
                else
                {
                    return Error("入库信息审核操作失败。");
                }
            }
            else if (status == "C")
            {
                V_CLK_44_MX_MaterialProListAcco_MidEntity midEntity = v_clk_44_mx_materialprolistacco_midApp.GetForm(keyValue);
                if (midEntity.FQuantity.HasValue && midEntity.FQuantity.Value > 0)
                {
                    bool result = false;
                    result = FriendStar.Repository.Infrastructure.ExecProcedure.InOutStoYclkStockCataRevoke(keyValue, OperatorProvider.Provider.GetCurrent().UserCode, FactoryFlag, FFacNum, "正式", out DocNum);
                    if (!string.IsNullOrEmpty(DocNum) && string.IsNullOrEmpty(entity.FDocuNO))
                    {
                        entity.FDocuNO = DocNum;
                    }
                    if (result)
                    {
                        return Success("入库信息冲红操作成功。", entity);
                    }
                    else
                    {
                        return Error("当前库存不足，入库信息冲红操作失败。");
                    }
                }
                else
                {
                    return Error("当前单据为冲红单，不可再进行冲红操作。");
                }
            }
            else
            {
                return Error("不可识别的操作类型。");
            }
        }

        #endregion

        /// <summary>
        /// 供应商材料
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="MaterialNO"></param>
        /// <param name="MaterialCode"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaterialListJson(Pagination pagination, string MaterialNO, string MaterialCode)
        {
            var data = new
            {
                rows = v_clk_91_gh_sumaterialApp.GetList(pagination, MaterialNO, MaterialCode),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        /// <summary>
        /// 材料合同
        /// </summary>
        /// <param name="CLFL"></param>
        /// <param name="CGCLPH"></param>
        /// <param name="CGHTBH"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaterialPurchaseListJson(string CLFL, string CGCLPH, string CGHTBH)
        {
            Pagination pagination = new Pagination()
            {
                page = 1,
                rows = 100,
                sidx = "CGHTBH,CGCLPH,CGCLGG"
            };
            var data = new
            {
                rows = v_mcg_11_ht_materialpurchasedj_daApp.GetTopList(pagination, CLFL, CGCLPH, CGHTBH),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        #region 领料

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult MaterialStorePick(string status)
        {
            status = string.IsNullOrEmpty(status) ? "0" : status;
            ViewBag.status = status;
            ViewBag.fhappencomList = zsj_b1_gh_supplierinformationApp.GetList("IsCLCustomer = '1'");
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaterialStorePickListJson(Pagination pagination, string FHappenCompanyNO, string MaterialCode, string MaterialSizes, string FCertificateNO, string FVoucherNo, string FBatchNo, string FCurDate)
        {
            var data = new
            {
                rows = v_clk_11_dj_materialregisterApp.GetList(pagination, MaterialStoreOutType, FHappenCompanyNO, MaterialCode, MaterialSizes, FCertificateNO, FVoucherNo, FBatchNo, FCurDate),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaterialStorePickDocListJson(Pagination pagination, string FHappenCompanyNO, string MaterialCode, string MaterialSizes, string FCertificateNO, string FVoucherNo, string FBatchNo, string FCurDate)
        {
            var data = new
            {
                rows = v_clk_12_dj_materialregister_midApp.GetList(pagination, MaterialStoreOutType, FHappenCompanyNO, MaterialCode, MaterialSizes, FCertificateNO, FVoucherNo, FBatchNo, FCurDate),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        #endregion

        #region 备料

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult MaterialStoreStock(string status)
        {
            status = string.IsNullOrEmpty(status) ? "0" : status;
            ViewBag.status = status;
            ViewBag.fhappencomList = zsj_b1_gh_supplierinformationApp.GetList("IsCLCustomer = '1'");
            ViewBag.fstoreList = cpk_zd_11_datstorehouseApp.GetSelectList().FindAll(t => t.FStoreType == "P2" || t.FStoreType == "P9");
            ViewBag.accountDate = GetDocumentNum.GetNum(UseUnit, FactoryFlag, "台账日期原材料", OperatorProvider.Provider.GetCurrent().UserCode, "", "临时");
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaterialStoreStockListJson(Pagination pagination, string FDocuNO, string FHappenComNO, string MaterialCode, string MaterialSizes, string FCertificateNO, string FVoucherNo, string FBatchNo, string FStoreNO, string FCurDate, string FAccountDate)
        {
            var data = new
            {
                rows = v_clk_31_bl_materialprematerialApp.GetList(pagination, MaterialStoreOutType, FDocuNO, FHappenComNO, MaterialCode, MaterialSizes, FCertificateNO, FVoucherNo, FBatchNo, FStoreNO, FCurDate, FAccountDate),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaterialStoreStockDocListJson(Pagination pagination, string FDocuNO, string FHappenCompanyNO, string MaterialCode, string MaterialSizes, string FCertificateNO, string FVoucherNo, string FBatchNo, string FStoreNO, string FCurDate, string FAccountDate)
        {
            string AccountDate = GetDocumentNum.GetNum(UseUnit, FactoryFlag, "台账日期原材料", OperatorProvider.Provider.GetCurrent().UserCode, "", "临时");
            if (AccountDate == FAccountDate)
            {
                var data = new
                {
                    rows = v_clk_32_bl_materialprematerial_midApp.GetList(pagination, MaterialStoreOutType, FDocuNO, FHappenCompanyNO, MaterialCode, MaterialSizes, FCertificateNO, FVoucherNo, FBatchNo, FStoreNO, FCurDate, FAccountDate),
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());
            }
            else
            {
                var data = new
                {
                    rows = v_clk_33_bl_materialprematerial_arcApp.GetList(pagination, MaterialStoreOutType, FDocuNO, FHappenCompanyNO, MaterialCode, MaterialSizes, FCertificateNO, FVoucherNo, FBatchNo, FStoreNO, FCurDate, FAccountDate),
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());
            }
        }

        #endregion

        #region 原材料出库
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult MaterialStoreOut(string status)
        {
            status = string.IsNullOrEmpty(status) ? "0" : status;
            ViewBag.status = status;
            ViewBag.fhappencomList = zsj_b1_gh_supplierinformationApp.GetList("IsCLCustomer = '1'");
            ViewBag.fstoreList = cpk_zd_11_datstorehouseApp.GetSelectList().FindAll(t => t.FStoreType == "P2" || t.FStoreType == "P9");
            ViewBag.accountDate = GetDocumentNum.GetNum(UseUnit, FactoryFlag, "台账日期原材料", OperatorProvider.Provider.GetCurrent().UserCode, "", "临时");
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaterialStoreOutListJson(Pagination pagination, string FDocuNO, string FHappenComNO, string MaterialCode, string MaterialSizes, string FStoreNO, string FCurDate, string FCredence, string FCertificateListID, string FAccountDate)
        {
            var data = new
            {
                rows = v_clk_42_mx_materialprolistaccoApp.GetList(pagination, MaterialStoreOutType, FAccountDate, FDocuNO, FHappenComNO, MaterialCode, MaterialSizes, FStoreNO, FCurDate, FCredence, FCertificateListID),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaterialStoreOutDocListJson(Pagination pagination, string FDocuNO, string FHappenComNO, string MaterialCode, string MaterialSizes, string FStoreNO, string FCurDate, string FCredence, string FCertificateListID, string FAccountDate)
        {
            string AccountDate = GetDocumentNum.GetNum(UseUnit, FactoryFlag, "台账日期原材料", OperatorProvider.Provider.GetCurrent().UserCode, "", "临时");
            if (AccountDate == FAccountDate)
            {
                var data = new
                {
                    rows = v_clk_44_mx_materialprolistacco_midApp.GetList(pagination, MaterialStoreOutType, FAccountDate, FDocuNO, FHappenComNO, MaterialCode, MaterialSizes, FStoreNO, FCurDate, FCredence, FCertificateListID),
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());
            }
            else
            {
                var data = new
                {
                    rows = v_clk_46_mx_materialprolistacco_arcApp.GetList(pagination, MaterialStoreOutType, FAccountDate, FDocuNO, FHappenComNO, MaterialCode, MaterialSizes, FStoreNO, FCurDate, FCredence, FCertificateListID),
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());
            }
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult MaterialStoreOutInput(string status, string number)
        {
            ViewBag.status = status != "B" ? "I" : status;
            ViewBag.number = (number == null) ? "" : number;
            ViewBag.fhappencomList = zsj_b1_gh_supplierinformationApp.GetList("IsCLCustomer = '1'");
            ViewBag.fdocutypenoList = bus_sj_dictionaryApp.GetItemList(MaterialStoreOutType);
            ViewBag.fdocunoTemp = GetDocumentNum.GetNum(UseUnit, FactoryFlag, "原材料入库", OperatorProvider.Provider.GetCurrent().UserCode, "", "临时");
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult MaterialStoreOutOrder()
        {
            ViewBag.fdocutypenoList = bus_sj_dictionaryApp.GetItemList(MaterialStoreOutType);
            ViewBag.fhappencomList = zsj_b1_gh_supplierinformationApp.GetList("IsCLCustomer = '1'");
            ViewBag.materialnoList = clde_zd_fl_cldeerialclassnoApp.GetList("", "");
            ViewBag.fprocompanyList = zsj_b1_gh_supplierinformationApp.GetList("IsMaterial = '1'");
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult MaterialStoreOutOrderEdit(string FDocuListID)
        {
            ViewBag.editObject = v_clk_42_mx_materialprolistaccoApp.GetForm(FDocuListID).ToJson();
            ViewBag.fdocutypenoList = bus_sj_dictionaryApp.GetItemList(MaterialStoreOutType);
            ViewBag.fhappencomList = zsj_b1_gh_supplierinformationApp.GetList("IsCLCustomer = '1'");
            ViewBag.materialnoList = clde_zd_fl_cldeerialclassnoApp.GetList("", "");
            ViewBag.fprocompanyList = zsj_b1_gh_supplierinformationApp.GetList("IsMaterial = '1'");
            return View();
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitMaterialStoreOutForm(string status, CLK_41_ML_MaterialProConAccoEntity entity, string detail, string keyValue)
        {
            string FFacNum = "";
            string DocNum = "";
            CLK_42_MX_MaterialProListAccoEntity detailEntity = null;
            if (entity != null)
            {
                if (entity.FDocuTypeNO != null && MaterialStoreTypeDictionary.ContainsKey(entity.FDocuTypeNO))
                {
                    FFacNum = MaterialStoreTypeDictionary[entity.FDocuTypeNO];
                }
                entity.FAccountDate = GetDocumentNum.GetNum(UseUnit, FactoryFlag, "台账日期原材料", OperatorProvider.Provider.GetCurrent().UserCode, "", "正式");
                detailEntity = detail.ToObject<CLK_42_MX_MaterialProListAccoEntity>();
            }
            if (status == "I")
            {
                //if (!string.IsNullOrEmpty(entity.FDocuNO))
                //{
                //    var list = clk_42_mx_materialprolistaccoApp.GetList(entity.FDocuNO, detailEntity.MaterialNO, detailEntity.MaterialCode, detailEntity.MaterialSizes, detailEntity.StandardNumber, detailEntity.MaterialDemand);
                //    if (list.Count > 0)
                //    {
                //        return Error("当前单据中已存在相同的原材料信息，出库信息输入操作失败。");
                //    }
                //}
                bool result = false;
                result = FriendStar.Repository.Infrastructure.ExecProcedure.InOutStoYclkStockCataInsert(UseUnit, entity, detailEntity, OperatorProvider.Provider.GetCurrent().UserCode, FactoryFlag, FFacNum, "正式", out DocNum);
                if (!string.IsNullOrEmpty(DocNum) && string.IsNullOrEmpty(entity.FDocuNO))
                {
                    entity.FDocuNO = DocNum;
                }
                if (result)
                {
                    return Success("出库信息输入操作成功。", entity);
                }
                else
                {
                    return Error("当前库存不足，出库信息输入操作失败。");
                }
            }
            else if (status == "D")
            {
                bool result = false;
                result = FriendStar.Repository.Infrastructure.ExecProcedure.InOutStoYclkStockCataDelete(keyValue, OperatorProvider.Provider.GetCurrent().UserCode);
                if (result)
                {
                    return Success("出库信息删除操作成功。", entity);
                }
                else
                {
                    return Error("出库信息删除操作失败。");
                }
            }
            else if (status == "U")
            {
                bool result = false;
                result = FriendStar.Repository.Infrastructure.ExecProcedure.InOutStoYclkStockCataUpdate(detailEntity.FDocuListID, detailEntity.FQuantity, detailEntity.FPrice, detailEntity.FZS, detailEntity.FDescription, OperatorProvider.Provider.GetCurrent().UserCode);
                if (result)
                {
                    return Success("出库信息修改操作成功。", entity);
                }
                else
                {
                    return Error("出库信息修改操作失败。");
                }
            }
            else if (status == "P")
            {
                bool result = false;
                foreach (string item in keyValue.Split(','))
                {
                    result = FriendStar.Repository.Infrastructure.ExecProcedure.InOutStoYclkStockCataCheck(UseUnit, item, OperatorProvider.Provider.GetCurrent().UserCode);
                }
                if (result)
                {
                    return Success("出库信息审核操作成功。", entity);
                }
                else
                {
                    return Error("出库信息审核操作失败。");
                }
            }
            else if (status == "C")
            {
                V_CLK_44_MX_MaterialProListAcco_MidEntity midEntity = v_clk_44_mx_materialprolistacco_midApp.GetForm(keyValue);
                if (midEntity.FQuantity.HasValue && midEntity.FQuantity.Value > 0)
                {
                    bool result = false;
                    result = FriendStar.Repository.Infrastructure.ExecProcedure.InOutStoYclkStockCataRevoke(keyValue, OperatorProvider.Provider.GetCurrent().UserCode, FactoryFlag, FFacNum, "正式", out DocNum);
                    if (!string.IsNullOrEmpty(DocNum) && string.IsNullOrEmpty(entity.FDocuNO))
                    {
                        entity.FDocuNO = DocNum;
                    }
                    if (result)
                    {
                        return Success("出库信息冲红操作成功。", entity);
                    }
                    else
                    {
                        return Error("当前库存不足，出库信息冲红操作失败。");
                    }
                }
                else
                {
                    return Error("当前单据为冲红单，不可再进行冲红操作。");
                }
            }
            else
            {
                return Error("不可识别的操作类型。");
            }
        }

        #endregion

        #region 原材料盘点
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult MaterialStoreInventory(string status)
        {
            status = string.IsNullOrEmpty(status) ? "0" : status;
            ViewBag.status = status;
            ViewBag.fhappencomList = zsj_b1_gh_supplierinformationApp.GetList("UnitNO=UseUnit");
            ViewBag.fstoreList = cpk_zd_11_datstorehouseApp.GetSelectList().FindAll(t => t.FStoreType == "P2" || t.FStoreType == "P9");
            ViewBag.accountDate = GetDocumentNum.GetNum(UseUnit, FactoryFlag, "台账日期原材料", OperatorProvider.Provider.GetCurrent().UserCode, "", "临时");
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaterialStoreInventoryListJson(Pagination pagination, string FDocuNO, string FHappenComNO, string MaterialCode, string MaterialSizes, string FStoreNO, string FCurDate, string FCredence, string FCertificateListID, string FAccountDate)
        {
            var data = new
            {
                rows = v_clk_42_mx_materialprolistaccoApp.GetList(pagination, MaterialStoreInventoryType, FAccountDate, FDocuNO, FHappenComNO, MaterialCode, MaterialSizes, FStoreNO, FCurDate, FCredence, FCertificateListID),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaterialStoreInventoryDocListJson(Pagination pagination, string FDocuNO, string FHappenComNO, string MaterialCode, string MaterialSizes, string FStoreNO, string FCurDate, string FCredence, string FCertificateListID, string FAccountDate)
        {
            string AccountDate = GetDocumentNum.GetNum(UseUnit, FactoryFlag, "台账日期原材料", OperatorProvider.Provider.GetCurrent().UserCode, "", "临时");
            if (AccountDate == FAccountDate)
            {
                var data = new
                {
                    rows = v_clk_44_mx_materialprolistacco_midApp.GetList(pagination, MaterialStoreInventoryType, FAccountDate, FDocuNO, FHappenComNO, MaterialCode, MaterialSizes, FStoreNO, FCurDate, FCredence, FCertificateListID),
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());
            }
            else
            {
                var data = new
                {
                    rows = v_clk_46_mx_materialprolistacco_arcApp.GetList(pagination, MaterialStoreInventoryType, FAccountDate, FDocuNO, FHappenComNO, MaterialCode, MaterialSizes, FStoreNO, FCurDate, FCredence, FCertificateListID),
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());
            }
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult MaterialStoreInventoryInput(string status, string number)
        {
            ViewBag.status = status != "B" ? "I" : status;
            ViewBag.number = (number == null) ? "" : number;
            ViewBag.fhappencomList = zsj_b1_gh_supplierinformationApp.GetList("UnitNO=UseUnit");
            ViewBag.fdocutypenoList = bus_sj_dictionaryApp.GetItemList(MaterialStoreInventoryType);
            ViewBag.fdocunoTemp = GetDocumentNum.GetNum(UseUnit, FactoryFlag, "原材料入库", OperatorProvider.Provider.GetCurrent().UserCode, "", "临时");
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult MaterialStoreInventoryOrder()
        {
            ViewBag.fdocutypenoList = bus_sj_dictionaryApp.GetItemList(MaterialStoreInventoryType);
            ViewBag.fhappencomList = zsj_b1_gh_supplierinformationApp.GetList("UnitNO=UseUnit");
            ViewBag.materialnoList = clde_zd_fl_cldeerialclassnoApp.GetList("", "");
            ViewBag.fprocompanyList = zsj_b1_gh_supplierinformationApp.GetList("IsMaterial = '1'");
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public ActionResult MaterialStoreInventoryOrderEdit(string FDocuListID)
        {
            ViewBag.editObject = v_clk_42_mx_materialprolistaccoApp.GetForm(FDocuListID).ToJson();
            ViewBag.fdocutypenoList = bus_sj_dictionaryApp.GetItemList(MaterialStoreInventoryType);
            ViewBag.fhappencomList = zsj_b1_gh_supplierinformationApp.GetList("UnitNO=UseUnit");
            ViewBag.materialnoList = clde_zd_fl_cldeerialclassnoApp.GetList("", "");
            ViewBag.fprocompanyList = zsj_b1_gh_supplierinformationApp.GetList("IsMaterial = '1'");
            return View();
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitMaterialStoreInventoryForm(string status, CLK_41_ML_MaterialProConAccoEntity entity, string detail, string keyValue)
        {
            string FFacNum = "";
            string DocNum = "";
            CLK_42_MX_MaterialProListAccoEntity detailEntity = null;
            if (entity != null)
            {
                if (entity.FDocuTypeNO != null && MaterialStoreTypeDictionary.ContainsKey(entity.FDocuTypeNO))
                {
                    FFacNum = MaterialStoreTypeDictionary[entity.FDocuTypeNO];
                }
                entity.FAccountDate = GetDocumentNum.GetNum(UseUnit, FactoryFlag, "台账日期原材料", OperatorProvider.Provider.GetCurrent().UserCode, "", "正式");
                detailEntity = detail.ToObject<CLK_42_MX_MaterialProListAccoEntity>();
            }
            if (status == "I")
            {
                //if (!string.IsNullOrEmpty(entity.FDocuNO))
                //{
                //    var list = clk_42_mx_materialprolistaccoApp.GetList(entity.FDocuNO, detailEntity.MaterialNO, detailEntity.MaterialCode, detailEntity.MaterialSizes, detailEntity.StandardNumber, detailEntity.MaterialDemand);
                //    if (list.Count > 0)
                //    {
                //        return Error("当前单据中已存在相同的原材料信息，盘点信息输入操作失败。");
                //    }
                //}
                bool result = false;
                result = FriendStar.Repository.Infrastructure.ExecProcedure.InOutStoYclkStockCataInsert(UseUnit, entity, detailEntity, OperatorProvider.Provider.GetCurrent().UserCode, FactoryFlag, FFacNum, "正式", out DocNum);
                if (!string.IsNullOrEmpty(DocNum) && string.IsNullOrEmpty(entity.FDocuNO))
                {
                    entity.FDocuNO = DocNum;
                }
                if (result)
                {
                    return Success("盘点信息输入操作成功。", entity);
                }
                else
                {
                    return Error("当前库存不足，盘点信息输入操作失败。");
                }
            }
            else if (status == "D")
            {
                bool result = false;
                result = FriendStar.Repository.Infrastructure.ExecProcedure.InOutStoYclkStockCataDelete(keyValue, OperatorProvider.Provider.GetCurrent().UserCode);
                if (result)
                {
                    return Success("盘点信息删除操作成功。", entity);
                }
                else
                {
                    return Error("盘点信息删除操作失败。");
                }
            }
            else if (status == "U")
            {
                bool result = false;
                result = FriendStar.Repository.Infrastructure.ExecProcedure.InOutStoYclkStockCataUpdate(detailEntity.FDocuListID, detailEntity.FQuantity, detailEntity.FPrice, detailEntity.FZS, detailEntity.FDescription, OperatorProvider.Provider.GetCurrent().UserCode);
                if (result)
                {
                    return Success("盘点信息修改操作成功。", entity);
                }
                else
                {
                    return Error("盘点信息修改操作失败。");
                }
            }
            else if (status == "P")
            {
                bool result = false;
                foreach (string item in keyValue.Split(','))
                {
                    result = FriendStar.Repository.Infrastructure.ExecProcedure.InOutStoYclkStockCataCheck(UseUnit, item, OperatorProvider.Provider.GetCurrent().UserCode);
                }
                if (result)
                {
                    return Success("盘点信息审核操作成功。", entity);
                }
                else
                {
                    return Error("盘点信息审核操作失败。");
                }
            }
            else
            {
                return Error("不可识别的操作类型。");
            }
        }

        #endregion

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaterialPro1ListJson(Pagination pagination, string MaterialNO, string MaterialCode)
        {
            var data = new
            {
                rows = v_clk_61_pc_materialproaiaoaccoApp.GetList1(pagination, MaterialNO, MaterialCode),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaterialPro2ListJson(string MaterialNO, string FVoucherNo, string MaterialCode)
        {
            Pagination pagination = new Pagination()
            {
                page = 1,
                rows = 100,
                sidx = "FVoucherNo,MaterialCode,MaterialSizes"
            };
            var data = new
            {
                rows = v_clk_61_pc_materialproaiaoaccoApp.GetList2(pagination, MaterialNO, FVoucherNo, MaterialCode),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetStoreHouseListJson(string FDocuTypeNO)
        {
            if (FDocuTypeNO == "LW")
            {
                var data = cpk_zd_11_datstorehouseApp.GetSelectList().FindAll(t => t.FStoreType == "P9");
                return Content(data.ToJson());
            }
            else 
            { 
                var data = cpk_zd_11_datstorehouseApp.GetSelectList().FindAll(t => t.FStoreType == "P2" || t.FStoreType == "P9");
                return Content(data.ToJson());            
            }
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetStoreHouseSetListJson(string storeNO)
        {
            var data = clk_zd_14_materialstorehousesetApp.GetList(storeNO);
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaterialStoreListByDocuNOJson(string FDocuNO, string FAccountDate)
        {
            string result = "";
            if (string.IsNullOrEmpty(FAccountDate))
            {
                var data = new
                {
                    rows = v_clk_42_mx_materialprolistaccoApp.GetListByDocuNO(FDocuNO)
                };
                result = data.ToJson();
            }
            else
            {

                string AccountDate = GetDocumentNum.GetNum(UseUnit, FactoryFlag, "台账日期原材料", OperatorProvider.Provider.GetCurrent().UserCode, "", "临时");
                if (AccountDate == FAccountDate)
                {
                    var data = new
                    {
                        rows = v_clk_44_mx_materialprolistacco_midApp.GetListByDocuNO(FDocuNO)
                    };
                    result = data.ToJson();
                }
                else
                {
                    var data = new
                    {
                        rows = v_clk_46_mx_materialprolistacco_arcApp.GetListByDocuNO(FDocuNO)
                    };
                    result = data.ToJson();
                }
            }
            return Content(result);
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaterialStoreItemByDocuNOJson(string FDocuNO, string FAccountDate)
        {
            string result = "";
            if (string.IsNullOrEmpty(FAccountDate))
            {
                var data = v_clk_42_mx_materialprolistaccoApp.GetItemByDocuNO(FDocuNO);
                result = data.ToJson();
            }
            else
            {

                string AccountDate = GetDocumentNum.GetNum(UseUnit, FactoryFlag, "台账日期原材料", OperatorProvider.Provider.GetCurrent().UserCode, "", "临时");
                if (AccountDate == FAccountDate)
                {
                    var data = v_clk_44_mx_materialprolistacco_midApp.GetItemByDocuNO(FDocuNO);
                    result = data.ToJson();
                }
                else
                {
                    var data = v_clk_46_mx_materialprolistacco_arcApp.GetItemByDocuNO(FDocuNO);
                    result = data.ToJson();
                }
            }
            return Content(result);
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaterialStoreTempFDocuNO(string FDocuTypeNO)
        {
            string FFacNum = "";
            if (MaterialStoreTypeDictionary.ContainsKey(FDocuTypeNO))
            {
                FFacNum = MaterialStoreTypeDictionary[FDocuTypeNO];
            }
            string result = GetDocumentNum.GetNum(UseUnit, FactoryFlag, FFacNum, OperatorProvider.Provider.GetCurrent().UserCode, "", "临时"); ;
            return Content(result);
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult GetPrintDetailsData(bool isDefaultDate, bool isArchive, string ids)
        {
            var data = app.GetPrintDetailsData(isDefaultDate, isArchive, ids);
            return Success("获取打印数据成功。", data);
        }
    }
}
